﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jumpton.SFA.Server.Entity;
using Jumpton.Persistent;
namespace Jumpton.SFA.Server.BusinessRule.System
{
    /// <summary>
    /// Status状态为1表示开启 状态为0表示关闭
    /// </summary>
    public class FinancialMonthManager : BusinessLogic
    {
        public static bool IsOKtoConfirmByFinacialMonth(DateTime dt,decimal domainID)
        {
            string date =dt.ToString("yyyy年MM月");
            ObjectRow obj = ObjectRow.Load(typeof(Financial_Month), "this.Date='" + date + "' and this.DOMAINID='" + domainID + "'");
            if (obj != null)
            {
                Financial_Month fmonth = (Financial_Month)obj;
                return (fmonth.Status =="1");
                
            }
            return true;
        }

        public static void CreateFinancialMonth(string usercode,decimal domainID)
        {
            DateTime dt = DateTime.Now;
            for (int i= 0 ; i <= 6 ; i++)
            {
                string date = dt.AddMonths(i).ToString("yyyy年MM月");
                ObjectRow obj = ObjectRow.Load(typeof(Financial_Month), "this.Date='" + date + "' and this.DOMAINID='" + domainID + "'");
                if (obj == null)
                {
                    Financial_Month fmonth = new Financial_Month();
                    fmonth.Date = date;
                    fmonth.Status = "1";
                    fmonth.DOMAINID = domainID;
                    fmonth.LAST_UPDATER = usercode;
                    fmonth.LAST_UPDATE_TIME = BusinessLogic.GetDateTimeDecimal(DateTime.Now);
                    fmonth.Save();
                }
            }
        }
        public ObjectTable GetByDate(string date,decimal domainID)
        {
            string sqldate ="";
            DateTime dt = new DateTime();
            if(DateTime.TryParse(date,out dt))
            {
                sqldate = dt.ToString("yyyy年MM月");
                return new ObjectTable(typeof(Financial_Month), "this.Date='" + sqldate + "' and DOMAINID='" + domainID + "'");
            }
            return new ObjectTable(typeof(Financial_Month), "this.DOMAINID='" + domainID + "'");

        }
    }
}

